﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XH.DataUpload.Logger
{
    public static class Logger
    {
        private static readonly string _logFilePath;
        private static readonly object _lockObject = new object(); // 用于线程安全的锁对象

        public static void WirteLog(string logpath, string message, LogLevel level = LogLevel.Info)
        {
            lock (_lockObject)
            {
                string fileName = "";
                StreamWriter rs = default(StreamWriter);
                try
                {
                    if (Directory.Exists(logpath) == false)
                    {
                        Directory.CreateDirectory(logpath);
                    }
                    fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                    var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                    var logEntry = $"{timestamp} [{level}] {message}{Environment.NewLine}";
                    rs = new StreamWriter(logpath + "\\" + fileName, true);
                    rs.WriteLine(logEntry);
                    rs.Close();
                }
                catch (Exception ex)
                {
                    string err = ex.Message;
                }
            }
        }

        // 日志级别枚举
        public enum LogLevel
        {
            Info,
            Warning,
            Error,
            Debug
        }
    }
}
